package stackAndQueue;

class Stack {
	public  int[] stack;
	public int r;
	public Stack(int n) {
		stack = new int[n];
		r = 0;
	}
	public void push(int val) {
		stack[r++] = val;
	}
	public int pop() {
		return stack[--r];
	}
	public int peek() {
		return stack[r-1];
	}
	public boolean isEmpty() {
		return r==0;
	}
	
	public int size() {
		return r;
	}
}


class MyStack {
	public static int MAX = 100;
	public static final int[] queue = new int[MAX];
	public static int r,l;
	public static int size;
    public MyStack() {
    	l = r = size = 0;
    }
    
    public void push(int x) {
    	int n = size;
    	queue[r++] = x;
    	r %= MAX;
    	size++;
    	while(n>0) {
    		queue[r++] = queue[l++];
    		l %= MAX;
    		--n;
    	}
    }
    
    public int pop() {
    	int ret = queue[l];
    	l = (l+1)%MAX;
    	size--;
    	return ret;
    }
    
    public int top() {
    	return queue[l];
    }
    
    public boolean empty() {
    	return size==0;
    }
}

class MyCircularDeque {
	
	public int[] queue;
	private int l,r,limit,size;
	//limit:队列的最大容量
	//size：队列的长度
	//[l,r) 队列区间。

    public MyCircularDeque(int k) {
    	queue = new int[k];
    	l = r = size = 0;
    	limit = k;
    }
    
    public boolean insertFront(int value) {
    	if(isFull()) {
    		return false;
    	}
    	queue[l = l==0?limit-1:l-1] = value;
    	size++;
    	return true;
    }
    
    public boolean insertLast(int value) {
    	if(isFull()) {
    		return false;
    	}
    	queue[r] = value;
    	r = r==limit-1?0:r+1;
    	size++;
    	return true;
    }
    
    public boolean deleteFront() {
    	if(isEmpty()) {
    		return false;
    	}
    	l = l==limit-1?0:l+1;
    	size--;
    	return true;
    }
    
    public boolean deleteLast() {
    	if(isEmpty()) {
    		return false;
    	}
    	r = r==0?limit-1:r-1;
    	size--;
    	return true;
    }
    
    public int getFront() {
    	return isEmpty()?-1:queue[l];
    }
    
    public int getRear() {
    	return isEmpty()?-1:queue[r==0?limit-1:r-1];
    }
    
    public boolean isEmpty() {
    	return size == 0;
    }
    
    public boolean isFull() {
    	return size == limit;
    }
}
